package cn.xuewenbao.demo.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;

import cn.xuewenbao.base.core.BaseController;
import cn.xuewenbao.demo.entity.Demo;
import cn.xuewenbao.demo.service.DemoService;

/**
 * @Description: 测试 demo 单表操作页面接口类
 * @Author: wd
 * @Date: 2021-3-21
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "测试 demo 单表操作")
@RestController
@RequestMapping("/test/demo")
public class DemoController extends BaseController<Demo, DemoService> {

	@Autowired
	private DemoService DemoService;

	@Autowired
	private RedisUtil redisUtil;

	/**
	 * 分页列表查询
	 *
	 * @param Demo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "获取Demo数据列表", notes = "获取所有Demo数据列表")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent = "/demoList")
	public Result<?> list(Demo Demo, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		QueryWrapper<Demo> queryWrapper = QueryGenerator.initQueryWrapper(Demo, req.getParameterMap());
		Page<Demo> page = new Page<Demo>(pageNo, pageSize);

		IPage<Demo> pageList = DemoService.page(page, queryWrapper);
		log.info("查询当前页：" + pageList.getCurrent());
		log.info("查询当前页数量：" + pageList.getSize());
		log.info("查询结果数量：" + pageList.getRecords().size());
		log.info("数据总数：" + pageList.getTotal());
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param Demo
	 * @return
	 */
	@PostMapping(value = "/add")
	@AutoLog(value = "添加测试DEMO")
	@ApiOperation(value = "添加DEMO", notes = "添加DEMO")
	public Result<?> add(@RequestBody Demo Demo) {
		DemoService.save(Demo);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param Demo
	 * @return
	 */
	@PutMapping(value = "/edit")
	@ApiOperation(value = "编辑DEMO", notes = "编辑DEMO")
	@AutoLog(value = "编辑DEMO", operateType = CommonConstant.OPERATE_TYPE_3)
	public Result<?> edit(@RequestBody Demo Demo) {
		DemoService.updateById(Demo);
		return Result.OK("更新成功！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "删除测试DEMO")
	@DeleteMapping(value = "/delete")
	@ApiOperation(value = "通过ID删除DEMO", notes = "通过ID删除DEMO")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		DemoService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	@ApiOperation(value = "批量删除DEMO", notes = "批量删除DEMO")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.DemoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	@ApiOperation(value = "通过ID查询DEMO", notes = "通过ID查询DEMO")
	public Result<?> queryById(@ApiParam(name = "id", value = "示例id", required = true) @RequestParam(name = "id", required = true) String id) {
		Demo Demo = DemoService.getById(id);
		return Result.OK(Demo);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 */
	@PostMapping(value = "/exportXls")
	@PermissionData(pageComponent = "/demoList")
	@ApiOperation(value = "导出excel", notes = "导出excel")
	public ModelAndView exportXls(HttpServletRequest request, Demo demo) {
		//获取导出表格字段
		String exportFields = DemoService.getExportFields();
		//分sheet导出表格字段
		return super.exportXlsSheet(request, demo, Demo.class, "单表模型", exportFields, 500);
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/importExcel")
	@ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, Demo.class);
	}

	// =====Redis 示例===============================================================================================

	/**
	 * redis操作 -- set
	 */
	@GetMapping(value = "/redisSet")
	@ApiOperation(value = "redis set操作", notes = "redis set操作")
	public void redisSet() {
		redisUtil.set("name", "张三" + DateUtils.now());
	}

	/**
	 * redis操作 -- get
	 */
	@GetMapping(value = "/redisGet")
	@ApiOperation(value = "redis get操作", notes = "redis get操作")
	public String redisGet() {
		return (String) redisUtil.get("name");
	}

	/**
	 * redis操作 -- setObj
	 */
	@GetMapping(value = "/redisSetObj")
	@ApiOperation(value = "redis setObj操作", notes = "redis setObj操作")
	public void redisSetObj() {
		Demo p = new Demo();
		p.setAge(10);
		p.setBirthday(new Date());
		p.setContent("hello");
		p.setName("张三");
		p.setSex("男");
		redisUtil.set("user-zdh", p);
	}

	/**
	 * redis操作 -- getObj
	 */
	@GetMapping(value = "/redisGetObj")
	@ApiOperation(value = "redis getObj操作", notes = "redis getObj操作")
	public Object redisGetObj() {
		return redisUtil.get("user-zdh");
	}

	/**
	 * redis操作 -- get
	 */
	@GetMapping(value = "/redis/{id}")
	@ApiOperation(value = "redis getById操作", notes = "redis getById操作")
	public Demo redisGetDemo(@PathVariable("id") String id) {
		Demo t = DemoService.getDemoById(id);
		log.info(t.toString());
		return t;
	}

	// ===Freemaker示例================================================================================

	/**
	 * freemaker方式 【页面路径： src/main/resources/templates】
	 *
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/html")
	public ModelAndView ftl(ModelAndView modelAndView) {
		modelAndView.setViewName("demo3");
		List<String> userList = new ArrayList<String>();
		userList.add("admin");
		userList.add("user1");
		userList.add("user2");
		log.info("--------------test--------------");
		modelAndView.addObject("userList", userList);
		return modelAndView;
	}

	// ==========================================动态表单 JSON接收测试===========================================
	@PostMapping(value = "/testOnlineAdd")
	public Result<?> testOnlineAdd(@RequestBody JSONObject json) {
		log.info(json.toJSONString());
		return Result.OK("添加成功！");
	}

	/*----------------------------------------外部获取权限示例------------------------------------*/

	/**
	 * 【数据权限示例 - 编程】mybatisPlus java类方式加载权限
	 *
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/mpList")
	@PermissionData(pageComponent = "/demoList")
	public Result<?> loadMpPermissonList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		QueryWrapper<Demo> queryWrapper = new QueryWrapper<Demo>();
		//编程方式，给queryWrapper装载数据权限规则
		QueryGenerator.installAuthMplus(queryWrapper, Demo.class);
		Page<Demo> page = new Page<Demo>(pageNo, pageSize);
		IPage<Demo> pageList = DemoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 【数据权限示例 - 编程】mybatis xml方式加载权限
	 *
	 * @param Demo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/sqlList")
	@PermissionData(pageComponent = "/demoList")
	public Result<?> loadSqlPermissonList(Demo Demo, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		IPage<Demo> pageList = DemoService.queryListWithPermission(pageSize, pageNo);
		return Result.OK(pageList);
	}
	/*----------------------------------------外部获取权限示例------------------------------------*/
}
